<?php

/**
 * lodging actions.
 *
 * @package    SchelkinoBiz
 * @subpackage lodging
 * @author     Delight
 */
class lodgingActions extends sfActions
{
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request)
    {
        $cri = new Criteria();

        $cri->add( LodgingPeer::IS_PUBLISH, true );

        $this->lodging = LodgingPeer::doSelect( $cri );
    }

    public function executeDescription(sfWebRequest $request)
    {
        $this->lodging = LodgingPeer::retrieveByPK( $request->getParameter( 'lodging', 0 ) );

        $cri = new Criteria();

        $cri->add( LodgingPhotoPeer::IS_FRONT, true );

        $this->front_photo = $this->lodging->getLodgingPhotos( $cri );

        $cri->add( LodgingPhotoPeer::IS_FRONT, false );

        $this->lodging_photos = $this->lodging->getLodgingPhotos( $cri );

        //$this->lodging_type = LodgingTypePeer::retrieveByPK( $this->lodging->getLodgingType() );
    }

    public function executeAjaxFilter(sfWebRequest $request)
    {
        $filterParams = $request->getPostParameters();

        $cri = new Criteria();

        $cri->add( LodgingPeer::IS_PUBLISH, true );

        $this->lodging = LodgingPeer::doFilterSelect( $cri, $filterParams );
        //$this->lodging = LodgingPeer::doSelect( $cri );
    }

    public function executeChangeCurrency(sfWebRequest $request)
    {
        $this->getUser()->setAttribute( 'currency', $request->getParameter( 'currency', sfConfig::get( 'app_default_currency' ) ) );

        $currency_ratio = array();

        foreach ( CurrencyPeer::doSelect( new Criteria() ) as $item )
        {
            $currency_ratio[ $item->getCurrency() ] = $item->getRatio();
        }

        $this->getUser()->setAttribute( 'currency_ratio', $currency_ratio );

        $this->redirect( $request->getReferer() );
    }

    public function executeUpdateCurrency(sfWebRequest $request)
    {
        $currency_ratio = array(
            'UAH' => 1,
            'USD' => 7.9105,
            'EUR' => 10.4123,
            'RUB' => 0.2650
        );

        $currency_base = sfConfig::get( 'app_default_currency' );

        $currency = CurrencyPeer::doSelect( new Criteria() );

        if ( !empty( $currency ) )
        {
            foreach ( $currency as $item )
            {
                $currency_item = $item->getCurrency();

                $item->setRatio( round( $currency_ratio[ $currency_item ] / $currency_ratio[ $currency_base ], 4 ) );

                $item->save();
            }
        }

        return sfView::NONE;
    }

    public function executeReserve(sfWebRequest $request)
    {
        return sfView::SUCCESS;
    }
}
